const dockerComposesBase: API.DockerComposeVO[] = [
  {
    id: "compose_nginx",
    name: "Nginx",
    status: "running",
    ports: [
      { host: 80, container: 80 },
      { host: 443, container: 443 }
    ],
    volumes: [
      { host: "/data/nginx/conf", container: "/etc/nginx" },
      { host: "/data/nginx/html", container: "/usr/share/nginx/html" }
    ],
    environment: [
      { key: "NGINX_PORT", value: "80" }
    ],
    restart: "always",
    networks: ["default"],
    image: "nginx:latest",
    containerName: "my-nginx",
  },
  {
    id: "compose_es",
    name: "Elasticsearch",
    status: "not-found",
    ports: [
      { host: 9200, container: 9200 },
      { host: 9300, container: 9300 }
    ],
    volumes: [
      { host: "/data/elasticsearch/data", container: "/usr/share/elasticsearch/data" },
      { host: "/data/elasticsearch/config", container: "/usr/share/elasticsearch/config" }
    ],
    environment: [
      { key: "ES_JAVA_OPTS", value: "-Xms2g -Xmx2g" },
      { key: "discovery.type", value: "single-node" }
    ],
    restart: "always",
    networks: ["default"],
    image: "elasticsearch:8.5.0",
    containerName: "elasticsearch-node",
  },
  {
    id: "compose_redis",
    name: "Redis",
    status: "stopped",
    ports: [{ host: 6379, container: 6379 }],
    volumes: [{ host: "/data/redis/data", container: "/data" }],
    environment: [
      { key: "REDIS_PASSWORD", value: "redis_pass" }
    ],
    restart: "on-failure:5",
    networks: ["backend"],
    image: "redis:alpine",
    containerName: "my-redis",
  },
];

const dockerComposesApp: API.DockerComposeVO[] = [
  {
    id: "compose_billing",
    name: "账单服务",
    status: "running",
    ports: [{ host: 8080, container: 8080 }],
    volumes: [
      { host: "/data/billing/logs", container: "/var/log/billing" },
      { host: "/data/billing/config", container: "/etc/billing" }
    ],
    environment: [
      { key: "BILLING_DB_HOST", value: "db.example.com" },
      { key: "BILLING_DB_USER", value: "billing_user" },
      { key: "BILLING_DB_PASS", value: "billing_pass" }
    ],
    restart: "always",
    networks: ["default"],
    image: "billing-app:latest",
    containerName: "billing-app",
  },
  {
    id: "compose_filemanager",
    name: "文件服务",
    status: "stopped",
    ports: [{ host: 8081, container: 8081 }],
    volumes: [
      { host: "/data/filemanager/data", container: "/data" },
      { host: "/data/filemanager/logs", container: "/var/log/filemanager" }
    ],
    environment: [
      { key: "FILEMANAGER_USER", value: "filemanager_user" },
      { key: "FILEMANAGER_PASS", value: "filemanager_pass" }
    ],
    restart: "always",
    networks: ["default"],
    image: "filemanager-app:latest",
    containerName: "filemanager-app",
  },
  {
    id: "compose_points",
    name: "积分服务",
    status: "running",
    ports: [{ host: 8082, container: 8082 }],
    volumes: [
      { host: "/data/points/logs", container: "/var/log/points" },
      { host: "/data/points/config", container: "/etc/points" }
    ],
    environment: [
      { key: "POINTS_DB_HOST", value: "db.example.com" },
      { key: "POINTS_DB_USER", value: "points_user" },
      { key: "POINTS_DB_PASS", value: "points_pass" }
    ],
    restart: "always",
    networks: ["default"],
    image: "points-app:latest",
    containerName: "points-app",
  },
  {
    id: "compose_app1",
    name: "应用服务 1",
    status: "not-found",
    ports: [{ host: 9090, container: 9090 }],
    volumes: [
      { host: "/data/app1/config", container: "/etc/app1" },
      { host: "/data/app1/logs", container: "/var/log/app1" }
    ],
    environment: [
      { key: "APP1_DB_HOST", value: "db.example.com" },
      { key: "APP1_DB_USER", value: "app1_user" },
      { key: "APP1_DB_PASS", value: "app1_pass" }
    ],
    restart: "always",
    networks: ["default"],
    image: "app1-app:latest",
    containerName: "app1-app",
  },
  {
    id: "compose_app2",
    name: "应用服务 2",
    status: "not-found",
    ports: [{ host: 9091, container: 9091 }],
    volumes: [
      { host: "/data/app2/config", container: "/etc/app2" },
      { host: "/data/app2/logs", container: "/var/log/app2" }
    ],
    environment: [
      { key: "APP2_DB_HOST", value: "db.example.com" },
      { key: "APP2_DB_USER", value: "app2_user" },
      { key: "APP2_DB_PASS", value: "app2_pass" }
    ],
    restart: "always",
    networks: ["default"],
    image: "app2-app:latest",
    containerName: "app2-app",
  },
  {
    id: "compose_app3",
    name: "应用服务 3",
    status: "not-found",
    ports: [{ host: 9092, container: 9092 }],
    volumes: [
      { host: "/data/app3/config", container: "/etc/app3" },
      { host: "/data/app3/logs", container: "/var/log/app3" }
    ],
    environment: [
      { key: "APP3_DB_HOST", value: "db.example.com" },
      { key: "APP3_DB_USER", value: "app3_user" },
      { key: "APP3_DB_PASS", value: "app3_pass" }
    ],
    restart: "always",
    networks: ["default"],
    image: "app3-app:latest",
    containerName: "app3-app",
  }
];

const updateContainerStatus = (id: string, newStatus: 'running' | 'stopped') => {
  let container = dockerComposesBase.find(c => c.id === id);
  if (container) {
    container.status = newStatus;
    return true;
  }
  container = dockerComposesApp.find(c => c.id === id);
  if (container) {
    container.status = newStatus;
    return true;
  }
  return false;
};

// 添加网络列表数据
const networks = [
  { label: 'default', value: 'default' },
  { label: 'backend', value: 'backend' },
  { label: 'frontend', value: 'frontend' },
  { label: 'custom', value: 'custom' }
];

export default {
  'GET /docker-api/docker/composes': (req: any, res: any) => {
    const { type } = req.query;

    let data: API.DockerComposeVO[];
    if (type === "base") {
      data = dockerComposesBase;
    } else if (type === "app") {
      data = dockerComposesApp;
    } else {
      data = [];
    }
    res.json({
      success: true,
      data: data,
      code: 200,
    });
  },

  // 获取网络列表
  'GET /docker-api/docker/networks': (
      _: any,
      res: { json: (data: any) => void }
  ) => {
    res.json({
      success: true,
      data: networks,
      code: 200
    });
  },

  // 更新容器配置
  'PUT /docker-api/docker/container/:id': (
      req: { params: { id: string }, body: Partial<API.DockerComposeVO> },
      res: { json: (data: any) => void }
  ) => {
    const { id } = req.params;
    const updates = req.body;

    // 更新 base 容器
    let targetContainer = dockerComposesBase.find(c => c.id === id);
    if (targetContainer) {
      Object.assign(targetContainer, updates);
    }

    // 更新 app 容器
    if (!targetContainer) {
      targetContainer = dockerComposesApp.find(c => c.id === id);
      if (targetContainer) {
        Object.assign(targetContainer, updates);
      }
    }

    res.json({
      success: true,
      code: 200
    });
  },


  // 启动容器
  'POST /docker-api/docker/container/:id/start': (
      req: { params: { id: string } },
      res: { json: (data: any) => void }
  ) => {
    const { id } = req.params;
    const success = updateContainerStatus(id, 'running');
    res.json({
      success,
      code: success ? 200 : 404
    });
  },

  // 停止容器
  'POST /docker-api/docker/container/:id/stop': (
      req: { params: { id: string } },
      res: { json: (data: any) => void }
  ) => {
    const { id } = req.params;
    const success = updateContainerStatus(id, 'stopped');
    res.json({
      success,
      code: success ? 200 : 404
    });
  },

  // 重启容器
  'POST /docker-api/docker/container/:id/restart': (
      req: { params: { id: string } },
      res: { json: (data: any) => void }
  ) => {
    const { id } = req.params;
    const success = updateContainerStatus(id, 'running');
    res.json({
      success,
      code: success ? 200 : 404
    });
  },

  // 重建容器
  'POST /docker-api/docker/container/:id/rebuild': (
      req: { params: { id: string } },
      res: { json: (data: any) => void }
  ) => {
    const { id } = req.params;
    const success = updateContainerStatus(id, 'running');
    res.json({
      success,
      code: success ? 200 : 404
    });
  }
};



